package com.dean.ui.utils;

import com.dean.entity.ScreenInfo;
import com.dean.ui.constant.ConponentType;
import com.formdev.flatlaf.extras.components.FlatSplitPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;

/**
 * @ClassName UIUtils
 * @Description UI工具类
 * @Author dean
 * @Date 2021/10/3 10:03
 */
public class UiUtils {
	
	private static final Logger logger = LoggerFactory.getLogger(UiUtils.class);
	/**
	 * 屏幕分辨率
	 */
	public static int screenWidth = 1024;
	public static int screenHeight = 768;
	/**
	 * 去除边框后的屏幕分辨率
	 */
	public static int noBorderWidth = 1024;
	public static int noBorderHeight = 768;
	
	/**
	 * menu 宽 高
	 */
	public static int menuWidth = 120;

	/**
	 * menu Item 宽 高
	 */
	public static int menuItemWidth = 120;
	public static int menuItemHeight = 30;
	
	/**
	 * content width
	 */
	public static int contentWidth = 1000;
	public static int contentHeight = 700;

	/**
	 * 内边据a
	 */
	public static int inputWidth = 360;
	/**
	 * input 内边据
	 */
	public static int inputPadding = 10;

	/**
	 * 内容区总共分两列
	 */
	public static int colLen = 500;


	/*******************组件固定长度 start*************************/
	/**
	 * element padding width 正常情况都用这个
	 */
	public final static int elePadding = 10;

	/**
	 * element height 默认组件高度
	 */
	public final static int eleHeight = 30;

	/**
	 * lable 组件长度 默认
	 */
	public  final  static int labelWidth = 120;

	/**
	 * button 组件长度  默认120
	 */
	public final  static int buttonWidth = 120;

	/**
	 * 文本域 高度  默认300
	 */
	public  final  static int areatextHeight = 300;

	/**
	 * 默认拆分列
	 */
	private final static Integer DEFAULT_COL_SPAN = 2;

	/*******************组件固定长度 end*************************/

	// 获取panel
	public static JPanel panel(){
		// 初始化 panel
		JPanel panel = new JPanel();
		//添加新的panel
		panel.setBounds(0, 0, UiUtils.contentWidth,UiUtils.contentHeight);
		panel.setBorder(BorderFactory.createLineBorder(Color.darkGray,1));
		panel.setLayout(null);
		return panel;
	}

	
	public static ScreenInfo getScreenSize() {
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = screensize.width;
        int h = screensize.height;
        screenWidth = w;
        screenHeight = h;
        logger.debug("当前屏幕分辨率为width:{},height{}",screenWidth,screenHeight);
        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());

        w = w - (screenInsets.left + screenInsets.right);
        h = h - (screenInsets.top + screenInsets.bottom);
        noBorderWidth = w - 2;
        noBorderHeight = h - 20;
        logger.debug("当前屏幕去除边框后分辨率为width:{},height{}",noBorderWidth,noBorderHeight);
        calculateElementsSize();
        return new ScreenInfo(w, h);
    }

	/**
	 * 计算元素的size
	 */
	public static void calculateElementsSize() {
		menuItemWidth = noBorderWidth/12;
		menuWidth = menuItemWidth + 10;
		contentWidth = noBorderWidth - menuWidth ;
		contentHeight = noBorderHeight-30;
		colLen = contentWidth/2;
		inputWidth = (contentWidth - labelWidth*2 - elePadding*4)/2;
		logger.debug("contentwidth:{},inputWidth:{},menuItemWidth{},menuWidth{},colLen{}",contentWidth,inputWidth,menuItemWidth,menuWidth,colLen);
	}



	/**
	 * component  组件
	 * row 第几行
	 * col 第几列
	 * colspan 占几列 默认占一列
	 * type 类型 1.label  2.input 3.nolabelinput 4.areatext 5.noareatext
	 * buttonCol 表示在第几行第几列 第几个button
	 * @param component
	 */
	public static void setBounds(JComponent component, int startRow,int rowSpan, int startCol, int colspan, ConponentType conponentType){
		// x坐标
		int x = 0;
		// y坐标
		int y = 0;
		// 组件长度
		int w = 0;
		// 组件高度
		int h = 0;
		// 计算 计算组件 x
		// 情况 1 label 情况2 input  情况3 没有lable的input 暂时忽略
		switch (conponentType){
			case LABLE:
				x = elePadding*colspan + colLen*(startCol -1);
				y = elePadding*startRow + eleHeight*(startRow - 1);
				w = labelWidth;
				h = eleHeight*rowSpan;
				break;
			case INPUT:
				x = elePadding*colspan + labelWidth + colLen*(startCol -1);
				y = elePadding*startRow + eleHeight*(startRow - 1);
				w = colLen - elePadding*2 - labelWidth;
				if(startCol == 1 && colspan == 2){
					w = contentWidth - elePadding*3 - labelWidth;
				}
				h = eleHeight*rowSpan;
				break;
			case NOLABLEINPUT:
				x = elePadding*colspan + colLen*(startCol -1);
				y = elePadding*startRow + eleHeight*(startRow - 1);
				w = colLen - elePadding*2;
				h = eleHeight*rowSpan;
			case SPLITPANEL:
				x = elePadding*colspan + colLen*(startCol -1);
				y = elePadding*startRow + eleHeight*(startRow - 1);
				w = colLen - elePadding*2;
				h = eleHeight*rowSpan;
				break;
		}
		logger.debug("bounds[component={},x={},y={},w={},h={}]",component.getName(),x,y,w,h);
		component.setBounds(x,y,w,h);
	}

	/**
	 *  设置button 位置
	 * @param button  组件
	 * @param startRow 起始行数
	 * @param rowSpan  占用行数
	 * @param startCol  起始列
	 * @param btnCol    第几个button
	 */
	public static void setButtonBounds(JComponent button,int startRow,int rowSpan ,int startCol,int btnCol){
		int x = elePadding*btnCol + buttonWidth*(btnCol-1);
		if(startCol == 2){
			x = UiUtils.contentWidth - (elePadding*btnCol + buttonWidth*btnCol);
		}
		int y = elePadding*startRow + eleHeight*(startRow - 1);
		int w = buttonWidth;
		int h = eleHeight*rowSpan;
		logger.debug("bounds[button={},x={},y={},w={},h={}]",button.getName(),x,y,w,h);
		button.setBounds(x,y,w,h);
		// button 设置样式
		JButton jButton = null;
		if(button instanceof JButton){
			jButton = (JButton) button;
		}
//		if("查询".equals(jButton.getText())){
//			jButton.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));
//		}

	}

	public static void setRightButtonBounds(JButton button,int startRow ,int btncol){
		int rowSpan = 1;
		int startCol = 2;
		setButtonBounds(button,startRow,rowSpan,startCol,btncol);
	}

	public static void setLeftButtonBounds(JButton button,int startRow ,int btncol){
		int rowSpan = 1;
		int startCol = 1;
		setButtonBounds(button,startRow,rowSpan,startCol,btncol);
	}

	/**
	 *
	 * @param table
	 * @param row
	 */
	public static void setTableBounds(JScrollPane table,int row){
		// 将content length 按照等分 通用方案
		int x = elePadding ;
		int y = elePadding*row + eleHeight*(row - 1);
		int w = contentWidth - elePadding*2;
		int h = contentHeight - elePadding*(row+1) - eleHeight*(row - 1);
		logger.debug("bounds[table={},x={},y={},w={},h={}]",table.getName(),x,y,w,h);
		table.setBounds(x,y,w,h);
	}

	/**
	 * jCheckBox 控件处理
	 * @param jCheckBox
	 * @param startRow
	 * @param startCol
	 * @param boxNum
	 */
	public static void setJCheckBoxBounds(JCheckBox jCheckBox,int startRow,int startCol,int boxNum){
		int x = 0;
		int y = 0;
		int w = 0;
		int h = 0;
		if("全选".equals(jCheckBox.getText())||"反选".equals(jCheckBox.getText())){
			 x = elePadding*(boxNum)+(80)*(boxNum-1)+colLen*(startCol-1);
			 y = elePadding*(startRow)+eleHeight*(startRow-1);
			 w = 80;
			 h = eleHeight;
		}else{
			 x = elePadding*(boxNum)+(labelWidth+80)*(boxNum-3)+colLen*(startCol-1)+160;
			 y = elePadding*(startRow)+eleHeight*(startRow-1);
			 w = labelWidth+80;
			 h = eleHeight;
		}

		logger.debug("bounds[JCheckBox={},x={},y={},w={},h={}]",jCheckBox.getText(),x,y,w,h);
		jCheckBox.setBounds(x,y,w,h);
	}

	/**
	 * 默认 第一列 占 1 列 无 label
	 * @param textArea
	 * @param startRow
	 * @param rowSpan
	 */
	public static void setTextAreaBounds(JComponent textArea,int startRow,int rowSpan){
		setTextAreaBounds(textArea,startRow,rowSpan,1,1,false);
	}

	/**
	 * 默认 第一列 占 1 列 无 label
	 * @param textArea
	 * @param startRow
	 * @param rowSpan
	 */
	public static void setTextAreaBounds(JComponent textArea,int startRow,int rowSpan,boolean lable){
		setTextAreaBounds(textArea,startRow,rowSpan,1,1,lable);
	}

	/**
	 * 默认 第一列 占 2列 无 label
	 * @param textArea
	 * @param startRow
	 * @param rowSpan
	 */
	public static void setTextAreaBounds(JComponent textArea,int startRow,int rowSpan,int startCol){
		setTextAreaBounds(textArea,startRow,rowSpan,startCol,1,false);
	}

	/**
	 * 默认 第一列 占 2列 无 label
	 * @param textArea
	 * @param startRow
	 * @param rowSpan
	 */
	public static void setTextAreaBounds(JComponent textArea,int startRow,int rowSpan,int startCol,boolean lable){
		setTextAreaBounds(textArea,startRow,rowSpan,startCol,1,lable);
	}

	/**
	 * 默认 第一列 占 2列 无 label
	 * @param textArea
	 * @param startRow
	 * @param rowSpan
	 */
	public static void setTextAreaBounds(JComponent textArea,int startRow,int rowSpan,int startCol,int colSpan){
		setTextAreaBounds(textArea,startRow,rowSpan,startCol,colSpan,false);
	}

	/**
	 * 	组件高度以 eleHeight 为基准  内边距以 elePadding 为基准  屏幕一分为2
	 *  设置文本域大小
	 * @param textArea 文本域组件
	 * @param starRow      第几行
	 * @param rowSpan  占用几行  -1 为剩余的全部
	 * @param startCol      第几列
	 * @param colSpan  占用几列
	 * @param lable    是否有lable
	 */
	public static void setTextAreaBounds(JComponent textArea,int starRow,int rowSpan,int startCol, int colSpan,boolean lable){
		// x 坐标调整
		int x = elePadding;
		// have label
		if(lable){
			x = x*2+labelWidth;
		}
		// 第二列
		if(startCol == 2){
			x = colLen + x;
		}

		int y = elePadding*starRow + eleHeight*(starRow - 1);

		// 长度计算
		int w = colLen - x - elePadding;
		// 第一列 且 占两2列
		if(startCol == 1 && colSpan == 2){
			w = w + colLen;
		}
		// 行高  占几行
		int h = elePadding*(rowSpan-1) + eleHeight*rowSpan;
		if(rowSpan == -1){
			h = (contentHeight - eleHeight*(starRow-1) - elePadding*(starRow+1));
		}

		logger.debug("bounds[textArea={},x={},y={},w={},h={}]",textArea.getName(),x,y,w,h);
		textArea.setBounds(x,y,w,h);
	}

	public static void setSplitPanel(JComponent splitPanel,int startRow,int rowSpan,int startCol, int colSpan,boolean lable){
		// x 坐标调整
		int x = elePadding;
		// have label
		if(lable){
			x = x*2+labelWidth;
		}
		// 第二列
		if(startCol == 2){
			x = colLen + x;
		}

		int y = elePadding*startRow + eleHeight*(startRow - 1);

		// 长度计算
		int w = colLen - x - elePadding;
		// 第一列 且 占两2列
		if(startCol == 1 && colSpan == 2){
			w = w + colLen;
		}
		// 行高  占几行
		int h = elePadding*(rowSpan-1) + eleHeight*rowSpan;
		if(rowSpan == -1){
			h = (contentHeight - eleHeight*(startRow-1) - elePadding*(startRow+1));
		}

		logger.debug("bounds[textArea={},x={},y={},w={},h={}]",splitPanel.getName(),x,y,w,h);
		splitPanel.setBounds(x,y,w,h);
	}

	/**
	 * 默认按照两列拆分 且有label
	 * @param component
	 * @param row
	 */
	public static  void setLeftBounds(JComponent component,int row){
		int startCol = 1; // 1 第一列
		setBounds(component,row,1,startCol,1,getConponentType(component));
	}

	/**
	 * 默认按照两列拆分 且有label
	 * @param component
	 * @param startRow
	 */
	public static  void setLeftBounds(JComponent component,int startRow,int colSpan){
		int startCol = 1; // 1 第一列
		setBounds(component,startRow,1,startCol,colSpan,getConponentType(component));
	}


	/**
	 * 默认按照两列拆分 且有label
	 * @param component
	 * @param startRow
	 */
	public static  void setRightBounds(JComponent component,int startRow){
		int startCol = 2 ;
		setBounds(component,startRow,1,startCol,1,getConponentType(component));
	}

	private static ConponentType getConponentType(JComponent component){
		ConponentType type = ConponentType.LABLE;
		if(component instanceof JLabel){
			type = ConponentType.LABLE;
		}else if(component instanceof JTextField){
			type = ConponentType.INPUT;
		}else if(component instanceof JTextArea){
			type = ConponentType.AREATEXT;
		}else if(component instanceof JComboBox){
			type = ConponentType.INPUT;
		} else if(component instanceof JButton){
			type = ConponentType.BUTTON;
		} else if (component instanceof FlatSplitPane) {
			type = ConponentType.SPLITPANEL;
		}
		return type;
	}
}
